/* *
 * Copyright (c) 2004-2005 Mocha Almond Fudge AI, SIT KMUTT. All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 * 
 *  o Redistributions of source code must retain the above copyright notice, 
 *    this list of conditions and the following disclaimer. 
 *     
 *  o Redistributions in binary form must reproduce the above copyright notice, 
 *    this list of conditions and the following disclaimer in the documentation 
 *    and/or other materials provided with the distribution. 
 *     
 *  o Neither the name of Mocha Almond Fudge AI, SIT KMUTT nor the names of 
 *    its contributors may be used to endorse or promote products derived 
 *    from this software without specific prior written permission. 
 */
package jboard.model.ai;

import jboard.model.BoardPosition;
import jboard.model.IBoardConstant;
import jboard.util.MoveUtil;
import jboard.util.Utilities;

/**
 * <p>Calculate heuristic value to the given BoardPosition using MinimaxTree<br>
 * There are 2 heuristic function that we use:
 * <li>Calculate total number of token(pawn) on the BoardPosition
 * <li>Calculate bad move on the BoardPosition
 * @author Vashira Ravipanich, -NOP- 
 */
public class HeuristicCalculation extends HeuristicAI{
	/** Adjust the cost to make AI more intelligence*/
    public static int EVADE_COST = 4;
	public static int TOKEN_COST = 5; 
	
	private BoardPosition currentBoardPosition;
	private int pileStrategy;
	private BoardPosition previousBoardPosition;
	
	/**
	 * @param boardPosition
	 */
	public HeuristicCalculation(BoardPosition boardPosition) {
		this.currentBoardPosition = boardPosition;
	}
	
	/**
	 * Default constructor
	 */
	public HeuristicCalculation(int pileStrategy) {
		currentBoardPosition = new BoardPosition();
		this.pileStrategy = pileStrategy;
	}
	/**
	 * Calculate the heuristic value of given BoardPosition using heuristic function
	 * 
	 * @return heuristic value
	 */
	public int calculate(int whichTurn) {
		int result = 0;
		result += checkNoTokens(whichTurn);
		if(this.pileStrategy == IBoardConstant.TWO_PILE) // use defensive heuristic only in 2 pile
		    result += checkBadMove(whichTurn);
		
		return result;
	}
	
	/**
	 * Second Heuristic function, defensive mode
	 *
	 * @author Vashira Ravipanich
	 */
	private int checkBadMove(int whichTurn) {
	    int result = checkEatablePosition(this.currentBoardPosition, EVADE_COST, Utilities.oppositeColor(whichTurn)); // called super method
	    return result;
	}
	
	/**
	 * First Heuristic function, Aggressive mode
	 * 
	 * @param whichTurn
	 * @return
	 */
	private int checkNoTokens(int whichTurn) {
		int currentNoBlackToken = MoveUtil.findTokenList(this.currentBoardPosition, IBoardConstant.BLACK).size();
		int currentNoWhiteToken = MoveUtil.findTokenList(this.currentBoardPosition, IBoardConstant.WHITE).size();
		int result = 0;

		// Standard (Very agressive mode!)
		result = (currentNoBlackToken * TOKEN_COST) - (currentNoWhiteToken * TOKEN_COST);

		return result;
	}
	
	/**
	 * @return Returns the currentBoardPosition.
	 */
	public BoardPosition getCurrentBoardPosition() {
		return currentBoardPosition;
	}
	/**
	 * @return Returns the previousBoardPosition.
	 */
	public BoardPosition getPreviousBoardPosition() {
		return previousBoardPosition;
	}
	/**
	 * First heuristic function
	 * 
	 * @deprecated since 8-01-05
	 * @return
	 */
	private int randBetween1_100() {
		int result = 0;
		result = (int) (Math.random() * 100);
		return result;
	}
	/**
	 * @param currentBoardPosition The currentBoardPosition to set.
	 */
	public void setCurrentBoardPosition(BoardPosition currentBoardPosition) {
		this.currentBoardPosition = currentBoardPosition;
	}
	/**
	 * @param previousBoardPosition The previousBoardPosition to set.
	 */
	public void setPreviousBoardPosition(BoardPosition previousBoardPosition) {
		this.previousBoardPosition = previousBoardPosition;
	}
}
